package com.mca.framework.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月11日 10:46:42
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class ThenRunDemo {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        CompletableFuture.supplyAsync(() -> {

            //子任务
            try {
                int i=1/0;
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return 123;
        }, executorService).thenRunAsync(()->{
            //thenRunAsync 线程执行完后再执行，没有返回的结果
            System.out.println("run execute");
            executorService.shutdown();
        });

        //主任务
        System.out.println("main end ");

        //获取子任务结果
        /*try {
            Integer value = future.get();
            System.out.println(value);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        executorService.shutdown();*/
    }
}
